/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Farcast;

import javax.microedition.lcdui.*;
import java.util.*;
/**
 *
 * @author Jeroen Rikhof
 */
public class DisplayManager extends Stack {
	
	private Display display; // reference to Display object
	private Displayable mainDisplayable; // Main displayable for MIDlet
	private Alert alStackError; // Alert for error conditions
	
	public DisplayManager(Display display, Displayable mainDisplayable) {
		this.display = display; // only one display object per midlet, this is it
		this.mainDisplayable = mainDisplayable;
		
		// create an alert display when an error occurs
		alStackError = new Alert("Displayable Stack Error");
		alStackError.setTimeout (Alert.FOREVER);
		
	}
	
	public void pushDisplayable(Displayable newDisplayable) {
                this.push(display.getCurrent());
		display.setCurrent(newDisplayable);
	}

	public void home() {
		while (elementCount > 1) {
			pop();
		}
		display.setCurrent(mainDisplayable);
	}
	
	public void popDisplayable() {
		if (empty() == false) {
			display.setCurrent((Displayable) pop());
		} else {
			display.setCurrent(alStackError, mainDisplayable);
		}
	}
}
